package com.cy.game

import java.lang.RuntimeException

class Proverb(
        val xIdx: Int,
        val yIdx: Int,
        val content: String,
        val orientation: Orientation,
        val initStateArr: IntArray
) {
    enum class Orientation {
        Hori,
        Vert
    }

    fun contains(x:Int,y:Int):Boolean{
        return if (orientation == Proverb.Orientation.Hori){
            // 水平
            (x == xIdx &&
                    (y == yIdx || y == yIdx + 1 || y == yIdx + 2 || y == yIdx + 3))
        }else{
            // 垂直
            (y == yIdx &&
                    (x == xIdx || x == xIdx - 1 || x == xIdx - 2 || x == xIdx - 3))
        }
    }

    // 填满不代表正确,只代表格子里面有值
    fun isFill(board: Board): Boolean {
        val lstContent = board.lstContent
        if (orientation == Proverb.Orientation.Hori){
            for (i in 0..3){
                if (lstContent[xIdx][yIdx + i].value == ""){
                    return false
                }
            }
        }else{
            for (i in 0..3){
                if (lstContent[xIdx - i][yIdx].value == ""){
                    return false
                }
            }
        }
        return true
    }

    fun findFirstEmptyPos(board: Board):Pair<Int,Int>{
        val lstContent = board.lstContent
        if (orientation == Orientation.Hori){
            for (i in 0..3){
                if (lstContent[xIdx][yIdx + i].value == ""){
                    return xIdx to (yIdx + i)
                }
            }
        }else{
            for (i in 0..3){
                if (lstContent[xIdx - i][yIdx].value == ""){
                    return (xIdx - i) to yIdx
                }
            }
        }
        throw RuntimeException("findFirstEmptyPos  error!")
    }

    fun findEmptyItem(board: Board):Item{
        val lstContent = board.lstContent
        // 1. 直接找当前单词附近位置的？
        // 2. 如果是行  则选择周围的行
        // 3. 如果是列  则选择周围的列
        if (orientation == Orientation.Hori){
            // 找下面的行
            for (i in xIdx downTo 0){
                val rows = lstContent[i]
                val ret = rows.find { item->
                    return@find item.enable && item.value == ""
                }
                if (ret != null){
                    return  ret
                }
            }

            // 找上面的行
            for (i in (xIdx+1) until Board.ContentRow){
                val rows = lstContent[i]
                val ret = rows.find { item->
                    return@find item.enable && item.value == ""
                }
                if (ret != null){
                    return  ret
                }
            }
        }else{
            // 列
            // 找左边的列
            for (j in yIdx downTo 0){
                lstContent.forEach { arr->
                    if (arr[j].enable && arr[j].value == ""){
                        return arr[j]
                    }
                }
            }

            // 右边的行
            for(j in (yIdx + 1) until Board.ContentColumn){
                lstContent.forEach { arr->
                    if (arr[j].enable && arr[j].value == ""){
                        return arr[j]
                    }
                }
            }
        }

        throw RuntimeException("格子全部都填满了,找不到一个空位置!")
    }

    // 这个词填入了几个字
    fun fillCount(board:Board): Int {
        val lstContent = board.lstContent
        var count = 0
        if (orientation == Orientation.Hori){
            for (i in 0..3){
                if (lstContent[xIdx][yIdx + i].value != ""){
                    count++
                }
            }
        }else{
            for (i in 0..3){
                if (lstContent[xIdx - i][yIdx].value != ""){
                    count++
                }
            }
        }
        return count
    }

    fun isRightFill(board:Board): Boolean {
        val winTable = board.winTable
        val lstContent = board.lstContent
        if (orientation == Orientation.Hori){
            for (i in 0..3){
                val k = xIdx * Board.ContentColumn + (yIdx + i)
                if (winTable[k] != lstContent[xIdx][yIdx + i].value ){
                    return false
                }
            }
        }else{
            for (i in 0..3){
                val k = (xIdx - i) * Board.ContentColumn + yIdx
                if (winTable[k] != lstContent[xIdx - i][yIdx].value){
                    return false
                }
            }
        }
        return true
    }

    fun getItem0(board: Board):Item{
        return board.lstContent[xIdx][yIdx]
    }
    fun getItem1(board: Board):Item{
        return if (orientation == Orientation.Hori) board.lstContent[xIdx][yIdx + 1] else board.lstContent[xIdx - 1][yIdx]
    }
    fun getItem2(board: Board):Item{
        return if (orientation == Orientation.Hori) board.lstContent[xIdx][yIdx + 2] else board.lstContent[xIdx - 2][yIdx]
    }
    fun getItem3(board: Board):Item{
        return if (orientation == Orientation.Hori) board.lstContent[xIdx][yIdx + 3] else board.lstContent[xIdx - 3][yIdx]
    }

    fun playRightAnim(board:Board,dt:Float){
        getItem0(board).playConfirmAnim(dt)
        getItem1(board).playConfirmAnim(dt + 0.1f)
        getItem2(board).playConfirmAnim(dt + 0.2f)
        getItem3(board).playConfirmAnim(dt + 0.3f)
    }
    fun playWrongAnim(board: Board){
        getItem0(board).playErrorAnim()
        getItem1(board).playErrorAnim()
        getItem2(board).playErrorAnim()
        getItem3(board).playErrorAnim()
    }

    // 找出所有空的和填错的格子
    fun findEmptyOrWrongItem(board: Board,arr:ArrayList<Item>){
        val winTable = board.winTable
        val item0 = getItem0(board)
        val item1 = getItem1(board)
        val item2 = getItem2(board)
        val item3 = getItem3(board)

        if (item0.value != winTable[item0.getComposeIdx()]){
            arr.add(item0)
        }
        if (item1.value != winTable[item1.getComposeIdx()]){
            arr.add(item1)
        }
        if (item2.value != winTable[item2.getComposeIdx()]){
            arr.add(item2)
        }
        if (item3.value != winTable[item3.getComposeIdx()]){
            arr.add(item3)
        }
    }
}
